import 'package:flutter/material.dart';
import 'package:flutter_wan_android/arch/ui/toast.dart';
import 'package:flutter_wan_android/page/qa.dart';
import 'package:flutter_wan_android/page/system.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:flutter_wan_android/res/theme.dart';

import '../r.dart';
import 'home.dart';
import 'mine.dart';

class Index extends StatefulWidget {
  @override
  _IndexState createState() => _IndexState();
}

class _IndexState extends State<Index> with WidgetsBindingObserver{
  var currentIndex = 0;


  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addObserver(this);//注册监听器
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance?.removeObserver(this);//移除监听器
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    print("========state================$state");

    if(state == AppLifecycleState.paused){
      ToastUtil.show("wanandroid 处于后台应用");
    }
  }

  var items = [
    BottomNavigationBarItem(
        icon: ImageIcon(AssetImage(R.imagesIcTabHome)), label: "首页"),
    BottomNavigationBarItem(
        icon: ImageIcon(AssetImage(R.imagesIcTabQa)), label: "问答"),
    BottomNavigationBarItem(
        icon: ImageIcon(AssetImage(R.imagesIcTabSystem)), label: "体系"),
    BottomNavigationBarItem(
        icon: ImageIcon(AssetImage(R.imagesIcTabMine)), label: "我的"),
  ];

  var pages = [HomePage(), QAPage(), SystemPage(), MinePage()];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(
      //   title: Text('Welcome to Flutter'),
      // ),
      body: IndexedStack(children: pages, index: currentIndex,),
      bottomNavigationBar: BottomNavigationBar(
        elevation: 1,
        iconSize: 20,
        currentIndex: currentIndex,
        onTap: (int index) {
          setState(() {
            currentIndex = index;
          });
        },
        unselectedItemColor: WColor.gray1,
        selectedItemColor: WColor.blue1,
        unselectedFontSize: 10,
        selectedFontSize: 10,
        items: items,
        type: BottomNavigationBarType.fixed,
        backgroundColor: context.wTheme.bottomBar,
      ),
    );
  }
}
